#!/usr/bin/env python

import wx
import __setting as setting

class Setting(wx.Dialog):
    '''Setting class
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, October 11, 2012. 10:25 PM'''
    
    _withFrequency = True
    
    def __init__(self, parent=None, withFrequency=True):
        wx.Dialog.__init__(self, parent)
        self._withFrequency = withFrequency
        
        self.Title = 'Setting'
        self.SetTransparent(240)
        
        formSizer = wx.BoxSizer(wx.HORIZONTAL)
        formSizer.AddSpacer(15)
        text = wx.StaticText(self, label='Device:')
        formSizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
        
        formSizer.AddSpacer(5)
        self._device = wx.TextCtrl(self)
        formSizer.Add(self._device, 1)
        
        if self._withFrequency:
            formSizer.AddSpacer(10)
            text = wx.StaticText(self, label='Sampling Rate:')
            formSizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
            
            formSizer.AddSpacer(5)
            choices = ['.125 ms', '.25 ms', '.5 ms', '1 ms', '2 ms', '4 ms']
            style = wx.CB_DROPDOWN | wx.CB_READONLY
            self._frequency = wx.ComboBox(self, choices=choices, style=style)
            formSizer.Add(self._frequency, 1)
        
        formSizer.AddSpacer(15)
        
        gainSizer = wx.BoxSizer(wx.HORIZONTAL)
        gainSizer.AddSpacer(15)
        text = wx.StaticText(self, label='Channel Gain:')
        gainSizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL)
        gainSizer.AddSpacer(5)
        choices = ['Uphill', 'Downhill', 'Mountain', 'Valley', 'Minimum',
                   'Maximum']
        style = wx.CB_DROPDOWN | wx.CB_READONLY
        self._gain = wx.ComboBox(self, choices=choices, style=style)
        gainSizer.Add(self._gain)
        
        channelSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.channels = {}
        channelSizer.AddSpacer(15)
        for index in range(1, 25):
            self.channels[index] = setting.Channel(self, '%s' % index, 5)
            channelSizer.Add(self.channels[index], 0, wx.EXPAND)
        channelSizer.AddSpacer(15)
        
        buttonSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddSpacer(5)
        sizer.Add(formSizer, 0, wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(gainSizer)
        sizer.AddSpacer(5)
        sizer.Add(channelSizer, 1, wx.EXPAND)
        sizer.AddSpacer(10)
        sizer.Add(buttonSizer, 0, wx.EXPAND | wx.RIGHT, 15)
        sizer.AddSpacer(5)
        
        self.SetSizerAndFit(sizer)
        
        # Set best size
        self.SetInitialSize()
        self.MinSize = self.Size
        # Lock width
        self.MaxSize = (self.Size[0], -1)
        
        self.bind()
        
    @property
    def device(self):
        return self._device.Value
    
    @device.setter
    def device(self, value):
        self._device.Value = '%s' % value
        
    @property
    def frequency(self):
        if not self._withFrequency: return ''
    
        return self._frequency.Value
    
    @frequency.setter
    def frequency(self, value):
        if self._withFrequency: self._frequency.Value = '%s' % value
        
    def bind(self):
        self._gain.Bind(wx.EVT_COMBOBOX, self._onGain)

    def _onGain(self, event):
        gain = event.String
        if gain == 'Uphill':
            gains = {1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 2, 7: 3, 8: 3, 9: 4,
                     10: 4, 11: 5, 12: 5, 13: 6, 14: 6, 15: 7, 16: 7, 17: 8,
                     18: 8, 19: 9, 20: 9, 21: 9, 22: 10, 23: 10, 24: 10}
        elif gain == 'Downhill':
            gains = {1: 10, 2: 10, 3: 10, 4: 9, 5: 9, 6: 9, 7: 8, 8: 8, 9: 7,
                     10: 7, 11: 6, 12: 6, 13: 5, 14: 5, 15: 4, 16: 4, 17: 3,
                     18: 3, 19: 2, 20: 2, 21: 2, 22: 1, 23: 1, 24: 1}
        elif gain == 'Mountain':
            gains = {1: 1, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8,
                     10: 9, 11: 10, 12: 10, 13: 10, 14: 10, 15: 9, 16: 8, 17: 7,
                     18: 6, 19: 5, 20: 4, 21: 3, 22: 2, 23: 1, 24: 1}
        elif gain == 'Valley':
            gains = {1: 10, 2: 10, 3: 9, 4: 8, 5: 7, 6: 6, 7: 5, 8: 4, 9: 3,
                     10: 2, 11: 1, 12: 1, 13: 1, 14: 1, 15: 2, 16: 3, 17: 4,
                     18: 5, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 10}
        elif gain == 'Minimum':
            gains = {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1,
                     10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1,
                     18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1}
        elif gain == 'Maximum':
            gains = {1: 10, 2: 10, 3: 10, 4: 10, 5: 10, 6: 10, 7: 10, 8: 10,
                     9: 10, 10: 10, 11: 10, 12: 10, 13: 10, 14: 10, 15: 10,
                     16: 10, 17: 10, 18: 10, 19: 10, 20: 10, 21: 10, 22: 10,
                     23: 10, 24: 10}
        else:
            pass
        for index, gain in gains.iteritems():
            self.channels[index].gain = gain
        event.Skip()